package com.qut.graduate.busrtquery.utils.store;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.qut.graduate.busrtquery.R;

/**
 * Sharedprefrence保存工具类
 */
public class SPUtils {

    private static SPUtils instance;
    private SharedPreferences SP;

    private SPUtils(Context mContext) {
        SP = PreferenceManager.getDefaultSharedPreferences(mContext);
    }

    public static SPUtils getInstance(Context context) {
        if (instance == null) {
            synchronized (SPUtils.class) {
                if (instance == null)
                    instance = new SPUtils(context);
            }
        }
        return instance;
    }

    public SharedPreferences.Editor getEditor() {
        return SP.edit();
    }

    // 是否是第一次登录进入用户中心，只有登录后第一次进入用户中心更新bmob的收藏数据到本地，true为第一次
    public boolean getIsFirstLoginToUserCenter(){
        return getBoolean("isFirstLoginToUserCenter", true);
    }

    public boolean setIsFirstLoginToUserCenter(boolean isFirstLoginToUserCenter){
        return putBoolean("isFirstLoginToUserCenter",isFirstLoginToUserCenter);
    }

    // 是否是第一次广告页，只有登录后第一次加载本地的图片，之后判断bmob是否含有广告，若有则下载加载广告图片
    public boolean getIsFirstOpenApp(){
        return getBoolean("isFirstOpenApp", true);
    }

    public boolean setIsFirstOpenApp(boolean isFirstOpenApp){
        return putBoolean("isFirstOpenApp",isFirstOpenApp);
    }

    public boolean isFirstTime(String key) {
        if (getBoolean(key, false)) {
            return false;
        } else {
            putBoolean(key, true);
            return true;
        }
    }

    public boolean contains(String key) {
        return SP.contains(key);
    }

    public boolean remove(String key) {
        return getEditor().remove(key).commit();
    }

    public int getInt(String key, int defaultValue) {
        return SP.getInt(key, defaultValue);
    }

    public long getLong(String key, long defaultValue) {
        return SP.getLong(key, defaultValue);
    }

    public Long getLong(String key, Long defaultValue) {
        if (SP.contains(key)) {
            return SP.getLong(key, 0);
        } else {
            return null;
        }
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return SP.getBoolean(key, defaultValue);
    }

    public String getString(String key, String defaultValue) {
        return SP.getString(key, defaultValue);
    }

    public boolean putInt(String key, int pValue) {
        return getEditor().putInt(key, pValue).commit();
    }

    public boolean putLong(String key, long pValue) {
        return getEditor().putLong(key, pValue).commit();
    }

    public boolean putBoolean(String key, boolean pValue) {
        return getEditor().putBoolean(key, pValue).commit();
    }

    public boolean putString(String key, String pValue) {
        return getEditor().putString(key, pValue).commit();
    }

    public int getCurrentTheme() {
        return getInt("app_theme", R.style.AppTheme);
    }

    public void setCurrentTheme(int currentTheme) {
        putInt("app_theme", currentTheme);
    }

}